package com.version.manager.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.version.manager.entity.User;
import com.version.manager.vo.JwtAuthenticationRequest;
import com.version.manager.vo.JwtTokenData;

import java.io.UnsupportedEncodingException;
import java.util.Date;

public class JWTUtil {
    //过期时间为1小时
    private static final long EXPIRE_TIME=60*60*1000;
    //
    private static final long BEFORE_TIME=20*60*1000;
    //密钥
    private static final String SECRET="026479DBF5E1";

    /**
     * 创建token
     * @param username
     * @return
     */
    public static JwtTokenData createToken(String companycode, String username, String password){
        try{
            Date expire=new Date(System.currentTimeMillis()+EXPIRE_TIME);
            Date refesh=new Date(expire.getTime()-BEFORE_TIME);
            Algorithm algorithm=Algorithm.HMAC256(SECRET);
            String jwt= JWT.create().withClaim("companycode",companycode)
                    .withClaim("username",username)
                    .withClaim("password",password)
                    .withExpiresAt(expire)
                    .sign(algorithm);
            JwtTokenData data=new JwtTokenData();
            data.setToken(jwt);
            data.setExpireDate(expire);
            data.setRefeshDate(refesh);
            return data;
        }catch (Exception e){
            return null;
        }
    }

    public static JwtTokenData refeshToken(String token){
        JwtAuthenticationRequest info=getJwtInfo(token);
        return createToken(info.getCompanyCode(),info.getUserName(),info.getPassWord());
    }

    /**
     * 校验token
     * @param token
     * @param jwt
     * @return
     */
    public static boolean verify(String token,JwtAuthenticationRequest jwt){
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            //在token中附带了userid信息
            JWTVerifier verifier = JWT.require(algorithm)
                    .withClaim("companycode",jwt.getCompanyCode())
                    .withClaim("username", jwt.getUserName())
                    .withClaim("password",jwt.getPassWord())
                    .build();
            //验证 token
            verifier.verify(token);
            return true;
        } catch (Exception exception) {
            return false;
        }
    }


    /**
     * 从token中获取信息
     * @param token
     * @return
     */
    public static JwtAuthenticationRequest getJwtInfo(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            String companycode=jwt.getClaim("companycode").asString();
            String username=jwt.getClaim("username").asString();
            String password=jwt.getClaim("password").asString();
            JwtAuthenticationRequest info=new JwtAuthenticationRequest(companycode,username,password);
            return info;
            //return jwt.getClaim("username").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

}
